﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SGArchiveSystem7xDB.Linq
{
    public class T_ArchiveWordDal
    {
        static SGArchiveSystem7xEntities conn;
        /// <summary>
        /// 添加一个词典内容
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static _ReturnValue Add_ArchiveWord(T_ArchiveWord en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveWord.Select(c => c).Where(c => c.WD_NAME == en.WD_NAME && c.WD_TYPE == en.WD_TYPE).Count() > 0)
                    return _ReturnValue.Already;
                else
                {
                    conn.T_ArchiveWord.Add(en);
                    conn.SaveChanges();
                    return _ReturnValue.OK;
                }
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 修改一个词典内容
        /// </summary>
        /// <param name="id">要修改的ID</param>
        /// <param name="name">修改后的词典名称</param>
        /// <returns></returns>
        public static _ReturnValue Modify_ArchiveWord(T_ArchiveWord en)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                if (conn.T_ArchiveWord.Select(c => c).Where(c => c.WD_NAME == en.WD_NAME && c.WD_ID != en.WD_ID && c.WD_TYPE == en.WD_TYPE).Count() > 0)
                    return _ReturnValue.Already;
                var query = conn.T_ArchiveWord.Select(c=>c).Where(c => c.WD_ID == en.WD_ID);
                foreach (var n in query)
                {
                    SGArchiveSystem7xCommons.SGEntity.ModifyEntity(n, en, "WD_ID");
                } 
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 删除一个词典内容
        /// </summary>
        /// <param name="id">要删除的ID</param>
        /// <returns>如果返回 Already, 表示是系统的无法删除</returns>
        public static _ReturnValue Del_ArchiveWord(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveWord.Select(c => c).Where(c => c.WD_ID == id);
                foreach (var n in query)
                {
                    conn.T_ArchiveWord.Remove(n);
                }
                conn.SaveChanges();
                return _ReturnValue.OK;
            }
            catch { return _ReturnValue.Err; }
        }

        /// <summary>
        /// 获取词典内容
        /// </summary>
        /// <returns></returns>
        public static List<T_ArchiveWord> Get_ArchiveWord(out int count, int type = -1, int start = -1, int row = -1)
        {
            count = 0;
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.T_ArchiveWord.Select(c => c);
                if (type > -1)
                    query = query.Where(c => c.WD_TYPE == type);
                if (start != -1 && row != -1)
                {
                    count = query.Count();
                    return query.OrderBy(c => c.WD_ORDER).ThenBy(c => c.WD_ID).Skip(start).Take(row).ToList();
                }
                else
                {
                    var list = query.OrderBy(c => c.WD_ORDER).ThenBy(c => c.WD_ID).ToList();
                    count = list.Count();
                    return list;
                }
            }
            catch { return null; }
        }

        /// <summary>
        /// 获取单个数据
        /// </summary>
        /// <param name="id">ID’s This is pooster</param>
        /// <returns></returns>
        public static T_ArchiveWord Get_ArchiveWord(int id)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                return conn.T_ArchiveWord.Select(c => c).Where(c => c.WD_ID == id).Single();
            }
            catch { return null; }
        }
        public static List<V_ArchiveWord> Get_Word(string name)
        {
            try
            {
                conn = new SGArchiveSystem7xEntities();
                var query = conn.V_ArchiveWord.Select(c => c);
                if (name != null && name != "")
                    query.Where(c => c.WD_TYPE == 2);
                return query.ToList();
            }
            catch { return null; }
        }

    }
}
